<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* var student = {
        sno: "0001234",
        sname: "王老五",
        sex: "男",
      }; */
      var student = {
        sno: "0001234",
        sname: "王老五",
        sex: "男",
        score: [100, 99, 98, 90],
      };

      //  封装浅克隆的方法
      function cloneObj(obj) {
        var newObj = {};
        for (var key in obj) {
          //   console.log(key, obj[key]);
          newObj[key] = obj[key]; //此处不可以使用obj.key因为key是变量
        }
        return newObj;
      }
      var obj1 = cloneObj(student);
      console.log(obj1);
      //   修改obj1克隆过来的数据
      obj1.sname = "张三";

      //   以上代码说明克隆过来的对象的基本类型的属性的修改不会影响到原对象的数据
      obj1.score.shift();
      obj1.score.push(99);

      console.log("克隆过来的对象", obj1);
      console.log("原对象的数据", student);

    //   结论：浅拷贝只适合克隆基本数据类型
    </script>
  </body>
</html>
